package com.itfreer.ui.photoview;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.itfreer.ui.R;
import com.itfreer.utils.AlertUtils;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * 图片列表查看器
 */
public class PhotoListAdapter extends BaseAdapter {

    private List<String> files;
    private PhotoViewPager ower;
    private Context context;
    private Boolean readonly; // 只读，为true时隐藏添加按钮

    public PhotoListAdapter(Context context, PhotoViewPager ower, List<String> files, Boolean readonly) {
        super();
        this.context = context;
        this.ower = ower;
        this.files = files;
        this.readonly = readonly;
    }

    @Override
    public int getCount() {
        return files.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String path = files.get(position);
        if(path==null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_img_list_item_add, parent,false);
            ImageView imageView = (ImageView)convertView.findViewById(R.id.item_img);
            if(readonly) {
                imageView.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.VISIBLE);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ower.add();
                    }
                });
            }
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_img_list_item, parent,false);
            ImageView imageView = (ImageView)convertView.findViewById(R.id.item_img);
            ImageView itemVideo = (ImageView)convertView.findViewById(R.id.item_video);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    PhotoInfo info = new PhotoInfo();
                    info.setFiles(files);
                    info.setPosition(position);
                    bundle.putSerializable("info", info);
                    intent.putExtras(bundle);
                    intent.setClass(context, PhotoActivity.class);
                    context.startActivity(intent);
                }
            });

            // 查看是否为本地资源，不存在则为在线资源
            File curFile = new File(path);
            Boolean isOnline = false;
            if(!curFile.exists()) {
                isOnline = true;
            }

            String[] files = path.split("\\.");
            String objectKey = files[files.length-1];
            objectKey = objectKey.toLowerCase();
            if(objectKey.equals("jpg") || objectKey.equals("png") || objectKey.equals("jpeg") || objectKey.equals("gif")
                || objectKey.equals("bpm") || objectKey.equals("webp")) {
                if(isOnline) {
                    imageView.setImageURI(Uri.parse(path));
                } else {
                    imageView.setImageURI(Uri.fromFile(new File(path)));
                }
            } else if(objectKey.equals("mp3") || objectKey.equals("mp4") || objectKey.equals("3gp") || objectKey.equals("webm")) {
                if(isOnline) {
                    imageView.setImageURI(Uri.parse(path));
                } else {
                    imageView.setImageBitmap(getVideoThumbnail(path));
                }
                itemVideo.setVisibility(View.VISIBLE);
            }

            com.itfreer.ui.base.TextView delView = (com.itfreer.ui.base.TextView)convertView.findViewById(R.id.item_del);
            if(readonly) {
                delView.setVisibility(View.GONE);
            } else {
                delView.setVisibility(View.VISIBLE);
                delView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    AlertUtils.dialog(context, "系统提示", "确认删除吗？",
                        new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ower.del(position);
                            }
                        },
                        new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }
                    );
                    }
                });
            }
        }
        return convertView;
    }

    private Bitmap getVideoThumbnail(String videoUrl) {
        // 大小为96*96，改成MINI_KIND，大小为512 x 384
        Bitmap videoThumbnail = ThumbnailUtils.createVideoThumbnail(
                videoUrl, MediaStore.Video.Thumbnails.MICRO_KIND);
        return videoThumbnail;
    }
}
